
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:oi_ai_tool/common/IconFontIcons.dart';
import 'package:oi_ai_tool/config/ai_source.dart';
import 'package:oi_ai_tool/controller/oi_token_controller.dart';
import 'package:oi_ai_tool/widgets/oi_appbar.dart';

class OiToken extends StatefulWidget {
  const OiToken({super.key});

  @override
  State<OiToken> createState() => _OiToken();
}

class _OiToken extends State<OiToken> {
  final OiTokenController oiTokenController = Get.find<OiTokenController>();
  GlobalKey<FormState> formKey = GlobalKey<FormState>();
  @override
  void initState() {
    oiTokenController.formKey = formKey;
    super.initState();
  }

  @override
  dispose() {
    // oiTokenController.removeFormKey();
    //
    super.dispose();
    oiTokenController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // OiLogger.info(msg: "token:${oiTokenController.requestToken}");
    return GetBuilder<OiTokenController>(builder: (buildContext) {
      return Scaffold(
          appBar: OiAppbar(
            isBackHome: false,
            title: const Text("Token Settings"),
          ),
          body: Padding(
            padding: EdgeInsets.all(10.0.sp),
            child: Form(
              key: oiTokenController.formKey,
              child: Column(
                children: [
                  Padding(
                    padding: EdgeInsets.all(10.0.sp),
                    child: dorpdownList(),
                  ),
                  Visibility(
                      visible: oiTokenController.isBaidu,
                      child: tokenformText(
                          controller: oiTokenController.apikeyController,
                          placeholder: "请输入 ai 授权 api_key",
                          icon: Icons.key,
                          label: "api_key",
                          validator:
                              oiTokenController.defaultNotEmpetyValidator,
                          setfield: (val) => oiTokenController
                              .requestToken.apiKey = val!.trim())),
                  tokenformText(
                      controller: oiTokenController.secretKeyController,
                      placeholder: "请输入对应 ai  授权 secret",
                      icon: IconFontIcons.iconPwd,
                      label: "api_secret",
                      ispassword: true,
                      validator: oiTokenController.defaultNotEmpetyValidator,
                      setfield: (val) => oiTokenController
                          .requestToken.secretKey = val!.trim()),
                  Center(
                    child: Padding(
                      padding: EdgeInsets.all(10.sp),
                      child: CupertinoButton.filled(
                          // padding: EdgeInsets.fromLTRB(70.sp, 0, 70.sp, 0),
                          onPressed: oiTokenController.onPressed,
                          child: SizedBox(
                              width: 200.sp,
                              child: const Align(
                                alignment: Alignment.center,
                                child: Text("保存"),
                              ))),
                    ),
                  )
                ],
              ),
            ),
          ));
    });
  }

  DropdownButtonFormField<String> dorpdownList() {
    return DropdownButtonFormField(
        value: oiTokenController.requestToken.aiSource,
        validator: oiTokenController.defaultNotSelectEmpetyValidator,
        decoration: const InputDecoration(
            hintText: "请选 ai", labelText: "ai", prefixIcon: Icon(Icons.source)),
        items: dropdownMenuItemList(),
        onSaved: (newValue) =>
            oiTokenController.requestToken.aiSource = newValue,
        onChanged: oiTokenController.onSelectChanged);
  }

  dropdownMenuItemList() {
    List<DropdownMenuItem<String>> list = [];
    for (var element in AiSource.values) {
      list.add(DropdownMenuItem(
        value: element.type.toString(),
        child: Text(element.name.toString()),
      ));
    }
    return list;
  }

  Padding tokenformText({
    String? placeholder,
    IconData? icon,
    String? label,
    bool? ispassword,
    FormFieldValidator<String>? validator,
    FormFieldSetter<String>? setfield,
    TextEditingController? controller,
  }) {
    return Padding(
      padding: EdgeInsets.all(10.0.sp),
      child: TextFormField(
        controller: controller,
        validator: validator,
        obscureText: (ispassword != null && ispassword
            ? !oiTokenController.showPwd
            : false),
        onSaved: setfield,
        decoration: InputDecoration(
            hintText: placeholder ?? " ",
            prefixIcon: Icon(icon ?? Icons.hourglass_empty),
            labelText: label ?? "",
            suffixIcon: ispassword != null && ispassword
                ? CupertinoButton(
                    onPressed: oiTokenController.changeShow,
                    child: oiTokenController.onShowPwd(),
                  )
                : const Text("")),
      ),
    );
  }
}
